﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class TextareaEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var textarea = new UCTextarea { Text = value.ToString() };
                var elHost = new ElementHost
                {
                    Width = 460,
                    Height = 360,
                    Child = textarea
                };
                edSvc.DropDownControl(elHost);
                return textarea.Text;
            }
            return value;
        }
    }

    public class TextareaJsonEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var type = value.GetType();
                var textarea = new UCTextarea { Text = comm.JsonSerializer.Serialize(value, false, true) };
                var elHost = new ElementHost
                {
                    Width = 460,
                    Height = 360,
                    Child = textarea
                };
                edSvc.DropDownControl(elHost);
                return comm.JsonSerializer.Deserialize(type, textarea.Text);
            }
            return value;
        }
    }

    /// <summary>
    /// UCTextarea.xaml 的交互逻辑
    /// </summary>
    public partial class UCTextarea : UserControl
    {
        public string Text
        {
            get
            {
                return tb.Text;
            }
            set
            {
                tb.Text = value;
            }
        }

        public UCTextarea()
        {
            InitializeComponent();
        }
    }
}
